package com.zone.vehicle.superApps.view.home.app.search;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.zone.sdk.module.comment.entity.SearchResultEntity;
import com.zone.vehicle.superApps.R;
import com.zone.vehicle.superApps.databinding.SearchResultFragmentBinding;
import com.zone.vehicle.superApps.databinding.SearchResultsListItemBinding;
import com.zone.vehicle.superApps.model.HomeViewModel;
import com.zone.vehicle.superApps.model.ViewModelFactory;
import com.zone.vehicle.superApps.view.common.HandledFragment;
import com.zone.vehicle.superApps.view.common.listener.ActionEnum;
import com.zone.vehicle.superApps.view.common.listener.ActionListener;

import org.apache.commons.lang3.ObjectUtils;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import static com.zone.vehicle.superApps.view.common.listener.ActionEnum.ACTION_SEARCH;
import static com.zone.vehicle.superApps.view.home.HomeFragment.APP_DETAIL_FRAGMENT;

public class SearchResultFragment extends HandledFragment {

    private static final String TAG = SearchResultFragment.class.getSimpleName();

    private SearchResultFragmentBinding binding;

    protected HomeViewModel viewModel;

    private InnerAppAdapter innerAppAdapter;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = SearchResultFragmentBinding.inflate(inflater);
        init();
        return binding.getRoot();
    }

    private void init(){
        viewModel = new ViewModelProvider(requireActivity(), new ViewModelFactory()).get(HomeViewModel.class);
        innerAppAdapter = new InnerAppAdapter(new ArrayList<>());
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        binding.recyclerView.setAdapter(innerAppAdapter);
//        innerAppAdapter.setData(viewModel.getSearchAppList().getValue());
//        innerAppAdapter.notifyDataSetChanged();
        viewModel.getSearchAppList().observe(getViewLifecycleOwner(), new Observer<List<SearchResultEntity>>() {
            @Override
            public void onChanged(List<SearchResultEntity> appEntities) {
                innerAppAdapter.setData(appEntities);
                innerAppAdapter.notifyDataSetChanged();
            }
        });
        addListener(new ActionListener(){

            @Override
            public boolean onAction(ActionEnum action) {
                Log.d(TAG,"onAction="+action);
                if (action == ACTION_SEARCH){
                    return true;
                }
                return false;
            }

            @Override
            public boolean onPageChanged(String pageName) {
                return false;
            }
        });
    }

    private class InnerAppAdapter extends RecyclerView.Adapter<InnerAppAdapter.ViewHolder>{

        private List<SearchResultEntity> data;

        private SearchResultsListItemBinding binding;

        public InnerAppAdapter(List<SearchResultEntity> data) {
            this.data = data;
        }

        public void setData(List<SearchResultEntity> data) {
            this.data = data;
        }

        @NotNull
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            binding = SearchResultsListItemBinding.inflate(LayoutInflater.from(parent.getContext()));
            return new ViewHolder(binding.getRoot());
        }

        @Override
        public void onBindViewHolder(@NotNull ViewHolder holder, int position) {
            if (ObjectUtils.isEmpty(data)){
                return;
            }
            SearchResultEntity appEntity = data.get(position);
            binding.name.setText(appEntity.getName());
            //设置图片大小
            RoundedCorners roundedCorners = new RoundedCorners(50);
            RequestOptions options = RequestOptions.bitmapTransform(roundedCorners)
                    .placeholder(new ColorDrawable(Color.GRAY))//设置占位图
//                    .error(R.mipmap.logo_app)//设置异常图片
                    .override(300, 300);//降低内存消耗
            Glide.with(holder.itemView.getContext())
                    .asBitmap()
                    .load(appEntity.getLogo())
                    .apply(options)
                    .into(holder.icon);
            holder.description.setText(appEntity.getDesc());
            holder.download_count.setText(appEntity.getDownloadsNum());
            holder.size.setText(appEntity.getSize());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    viewModel.setCurrentApp(appEntity);
                    changePage(APP_DETAIL_FRAGMENT);
                }
            });
        }

        @Override
        public int getItemCount() {
            return data.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {

            private final ImageView icon;
            private final TextView name;
            private final TextView description;
            private final TextView download_count;
            private final TextView size;

            public ViewHolder(View itemView) {
                super(itemView);
                icon = itemView.findViewById(R.id.icon);
                name = itemView.findViewById(R.id.name);
                description = itemView.findViewById(R.id.description);
                download_count = itemView.findViewById(R.id.download_count);
                size = itemView.findViewById(R.id.size);
            }
        }
    }
    
}